<html>
<head>

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-language" content="ru-ru" />
<title>Список сотрудников (краткий)</title>

<style type="text/css">
a {font-family : Arial; text-decoration : none}
a:link {color : #000000}
a:visited {color : #000000}
a:active {color : #000000}
a:hover {color : #0000ff}
</style>

</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">

<TABLE width=100% align=center  cellpadding=0 cellspacing=0 border=1> <tr>

<h1>Список сотрудников (краткий)</h1>
<h4><a href="directory_long.php">Полная версия</a> | [Короткая версия]</h4>

<?php
// ===
// This PHP script is used to display list of Active Directory users on a webpage, grabbing it via LDAP connection.
// Evgeny Varnavsky 2009-2010
// http://code.google.com/p/aduserlist/
// LGPL license
// ===

// Turn on when debugging
// error_reporting(E_ALL ^ E_NOTICE);

// Get sort_by variable from URL
$sort_by=$_GET['sort_by'];

// Replacing special characters for LDAP IO
// see: RFC2254
function ldap_quote($str) {
        return str_replace(
                array( '\\', ' ', '*', '(', ')' ),
                array( '\\5c', '\\20', '\\2a', '\\28', '\\29' ),
                $str
        );
}

// Get URL of current page
$pagename = $_SERVER['PHP_SELF'];
if (!$sort_by)
{
// Setting default sorting by name
$sort_by = "displayname";
}
echo "<td  bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'><a href='$pagename?sort_by=telephonenumber'>Внут.</a></font></td>";
echo "<td  bgcolor='dfdfdf'>&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'><a href='$pagename?sort_by=description'>Рус. имя</a></font></td>";
echo "<td  bgcolor='dfdfdf'>&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'><a href='$pagename?sort_by=mobile'>Моб.</a></font></td>";
echo "<td  bgcolor='dfdfdf'>&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'><a href='$pagename?sort_by=title'>Должность</a></font></td>";
echo "</tr>";

// Including file with LDAP login, password and server settings
include('ldap_settings_inc.php');

// List of attributes to grab
$ldapattr = array("cn","displayname","samaccountname", "mail", "mobile", "telephonenumber", "description", "title", "department");

// OPTION
// LDAP base tree to use
$ldapbase = "DC=spotall,DC=local";

// Filter output by:

// OPTION
// Only users with set telephone numbers
$ldapfltr = "(&(objectClass=user)(telephoneNumber=*))";

// Only objects (not only users but contacts too) with set mobile and telephone numbers
// $ldapfltr = "(|(mobile=*)(telephoneNumber=*))";

// Search filter reference:
// http://msdn.microsoft.com/en-us/library/aa746475(VS.85).aspx
// http://support.microsoft.com/kb/555638

// Attribute reference 
// Use lowercase for attribute names
// https://fsuid.fsu.edu/admin/lib/WinADLDAPAttributes.html
// http://computerperformance.co.uk/Logon/LDAP_attributes_active_directory.htm

// Connect to LDAP server
$ldapconn = ldap_connect($ldaphost, $ldapport) or die("Could not connect to $ldaphost");

// Recommended settings for AD domain controllers
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ldapconn, LDAP_OPT_SIZELIMIT, 2000);

// Log to LDAP server
ldap_bind($ldapconn,$ldapuser,$ldappass) or die("Can not bind as $ldapuser");

// Grab data with sorting
$ldapsrch = ldap_search($ldapconn, $ldapbase, $ldapfltr, $ldapattr);
ldap_sort  ($ldapconn  , $ldapsrch  , "$sort_by");
$ldaprecs = ldap_get_entries($ldapconn, $ldapsrch);

echo "Пользователей получено из каталога Active Directory: <b>" . $ldaprecs["count"] . "</b><br/><hr>";

$time = date('H:i \o\n l, jS F Y e');
echo "Актуальность: $time";

if ($ldaprecs["count"] < 1)
{
    echo "<p>No users fetched from ldap. Check settings.";
}
else
// Fill variables with data
{
	echo "<hr>";
    for ($i=0; $i < $ldaprecs["count"]; $i++)
    {
	$mobile = $ldaprecs[$i]["mobile"][0];
	$telephonenumber = $ldaprecs[$i]["telephonenumber"][0];
	$description = $ldaprecs[$i]["description"][0];
	$displayname = $ldaprecs[$i]["displayname"][0];
	$title = $ldaprecs[$i]["title"][0];
	$company = $ldaprecs[$i]["company"][0];
	$mail = $ldaprecs[$i]["mail"][0];

// Change table row color	
if($bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
else{$bgcolor='#f1f1f1';}

// Populate table, but with some filtering
if (!($cn[0] == "~")&&!($displayname=="")&&!($title==""))
{
echo "<tr >";
echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$telephonenumber</font></td>"; 
echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'><a href=mailto:$mail>$displayname</a></font></td>"; 
echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$mobile</font></td>"; 
echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$title</font></td>"; 
echo "</font></td>"; 
echo "</tr>";
}
	}
}

ldap_unbind($ldapconn);

?>
</table>